package com.wan37.logic.chat.util;

import com.wan37.logic.chat.domain.vo.ChatVO;
import com.wan37.logic.handler.LogicServerHandler;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameChat;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import com.wan37.protobuf.message.internal.Internal;
import com.wan37.protobuf.util.ServerProtoUtils;
import io.netty.buffer.ByteBuf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 聊天路由网关
 *
 * @author : luoyong
 * @date : 2020-07-15 17:49
 **/
@Component
public class ChatRouteUtil {
    @Autowired
    private UserServiceCache userServiceCache;

    /**
     * 发送私聊历史消息
     *
     * @param code
     * @param userId
     * @param chatVOS
     */
    public void sendChatResponse(int code, String userId, List<ChatVO> chatVOS) {
        GameChat.ChatResponse.Builder crb = GameChat.ChatResponse.newBuilder();
        crb.setCode(code);
        for (ChatVO chatVO : chatVOS) {
            chatVOToChat(chatVO, crb);
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(
                Internal.DestType.Client, userId, PtoNum.CHATRESPONSE, crb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 发送私聊消息
     *
     * @param code
     * @param userId
     * @param chatVO
     */
    public void sendChatResponse(int code, String userId, ChatVO chatVO) {
        GameChat.ChatResponse.Builder crb = GameChat.ChatResponse.newBuilder();
        crb.setCode(code);
        chatVOToChat(chatVO, crb);
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(
                Internal.DestType.Client, userId, PtoNum.CHATRESPONSE, crb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 广播消息
     *
     * @param code
     * @param userId
     * @param chatVO
     */
    public void sendWorldPlayerChatBroadcast(int code, String userId, ChatVO chatVO) {
        GameChat.WorldPlayerChatBroadcast.Builder wpcbb = GameChat.WorldPlayerChatBroadcast.newBuilder();
        wpcbb.setSenderId(userId);
        for (String receiverId : userServiceCache.getUserServiceMap().keySet()
                .stream().filter(userId1 -> !userId.equals(userId1)).collect(Collectors.toList())) {
            GameEntity.stringMsg stringMsg = GameEntity.stringMsg.newBuilder().setStringId(receiverId).build();
            wpcbb.addReceiverId(stringMsg);
        }
        GameChat.ChatResponse.Builder crb = GameChat.ChatResponse.newBuilder();
        crb.setCode(code);
        chatVOToChat(chatVO, crb);
        wpcbb.setChatResponse(crb.build());
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(
                Internal.DestType.Gate, userId, PtoNum.WORlDPLAYERCHATBROADCAST, wpcbb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    private void chatVOToChat(ChatVO chatVO, GameChat.ChatResponse.Builder crb) {
        GameChat.Chat.Builder cb = GameChat.Chat.newBuilder();
        cb.setSenderId(chatVO.getSenderId());
//        cb.setSenderName(userServiceCache.getUserServiceMap().get(chatVO.getSenderId()).getUserVO().getName());
        cb.setContent(chatVO.getContent());
        cb.setTime(chatVO.getCreateTime());
        crb.addChat(cb.build());
    }

}
